/*
 * @Description: 
 * @Version: V1.0.0
 * @Autor: CHY
 * @Date: 2022-05-15 18:18:19
 * @LastEditors: CHY
 * @LastEditTime: 2022-05-15 18:18:37
 */
/**
         * @description: 输出斐波那契数列
         * @param xx范围内的斐波那契数列
         * @return {*}
         * @author: CHY
         */
function getFb(i) {
    function* fibonacci() {
        let [pre, cur] = [0, 1];
        for (; ;) {
            [pre, cur] = [cur, pre + cur];
            yield cur;
        }
    }
    for (let n of fibonacci()) {
        if (n > i)
            break;
        console.log(n);
    }
}
getFb(10)

//
/**
 * @description: 闭包方法，斐波那契数列求和
 * @param undefined
 * @return {*}
 * @author: CHY
 */
function fb(n) {
    if (n === 1 || n === 2) {
        return 1;
    }
    return fb(n - 1) + fb(n - 2);
}
console.log(fb(20));

/**
 * @description: 优化方法，斐波那契数列求和
 * @param undefined
 * @return {*}
 * @author: CHY
 */
function fb(n) {
    var a = [0, 1, 1]
    if (n < 0) throw Error('输入大于0的数')
    if (n >= 3) {
        for (i = 3; i <= n; i++) {
            a[i] = a[i - 1] + a[i - 2]
        }
    }
    return a[n]
}
console.log(fb(20));